package com.mareas.web.jsf.component;

import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.jsf.FacesContextUtils;

import com.mareas.web.model.AgenciaNacional;
import com.mareas.web.service.GenericService;

@ManagedBean
@Scope("request")
@Component
@FacesConverter(forClass=AgenciaNacional.class)
public class AgenciaConverter implements Converter {

	@Autowired
	private GenericService genericService;
	
	public Object getAsObject(FacesContext facescontext,
			UIComponent uicomponent, String s) {
		AgenciaNacional agenciaNacional = null;
		if (getGenericService() == null)
		{
			this.genericService = (GenericService) FacesContextUtils.getRequiredWebApplicationContext(facescontext).getBean("genericService");			
		}
		
		agenciaNacional = (AgenciaNacional) getGenericService().get(AgenciaNacional.class, Integer.parseInt(s));
		
		return agenciaNacional;
	}

	public String getAsString(FacesContext facescontext,
			UIComponent uicomponent, Object obj) {
		if(obj == null)
			return null;
		return String.valueOf(((AgenciaNacional) obj).getAgenciaNacionalId());
	}

	public GenericService getGenericService() {
		return genericService;
	}

	public void setGenericService(GenericService genericService) {
		this.genericService = genericService;
	}


}
